﻿using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace BWindingMachine
{
    public partial class ProductForm : Form
    {
        private bool _isOk = false;

        public string _pName = "";
        public string _pCode = "";

        public bool IsOk { get { return _isOk; } }

        public ProductForm()
        {
            InitializeComponent();
            this.Location = new Point((Screen.PrimaryScreen.Bounds.Width - this.Width) / 2, (Screen.PrimaryScreen.Bounds.Height - this.Height) / 2);
        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ProductForm_Load(object sender, EventArgs e)
        {
            try
            {
                const string sql = "SELECT * FROM tb_product";
                DataSet ds = SQLiteHelper.ExecuteDataset(sql);
                DataTable dt = ds.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    Queue que = new Queue();
                    que.Enqueue(dr["productcode"].ToString());
                    que.Enqueue(dr["productname"].ToString());
                    ClassListViewOperate.AddOneRow(ref listView_product, que);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void listView_product_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (!listView_product.Items[e.Index].Checked)
            {
                foreach (ListViewItem lv in listView_product.Items)
                {
                    if (lv.Checked)
                    {
                        lv.Checked = false;
                        lv.Selected = false;
                    }
                }
                listView_product.Items[e.Index].Selected = true;
            }
        }

        private void listView_product_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListViewItem lv in listView_product.Items)
            {
                if (lv.Selected)
                {
                    lv.Checked = true;
                }
                else
                {
                    if (listView_product.SelectedIndices.Count > 0)
                    {
                        if (lv.Checked)
                        {
                            lv.Checked = false;
                        }
                    }
                }
            }
        }

        private void btn_confirm_Click(object sender, EventArgs e)
        {
            
            foreach (ListViewItem item in listView_product.Items)
            {
                if (!item.Checked) continue;
                _isOk = true;
                _pCode = item.SubItems[0].Text.Trim();
                _pName = item.SubItems[1].Text.Trim();
                break;
            }
            this.Close();
        }

    }
}